﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.components.mainmenu {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.events.*;
    import reprise.controls.*;
    import reprise.tweens.*;
    import com.robertpenner.easing.*;
    import com.adobe.utils.*;

    public class ScrollPane extends UIComponent {

        protected var m_scrollContainer:UIComponent;
        protected var m_scrollDisplay:UIComponent;
        protected var m_scrollDistance:Number;
        protected var m_leftArrowButton:SimpleButton;
        protected var m_rightArrowButton:SimpleButton;
        protected var m_numberOfVisibleItems:int;
        protected var m_numberOfItems:uint;// = 0
        protected var m_scrollIndex:int;// = 0
        protected var m_items:Array;
        protected var m_isScrolling:Boolean;
        protected var m_scrollItemWidthPlusMargin:Number;

        public static const className:String = "ScrollPane";

        public function ScrollPane(_arg1:int){
            this.m_items = [];
            super();
            this.m_numberOfVisibleItems = _arg1;
        }
        public function addItem(_arg1:UIComponent):UIComponent{
            this.m_items.push(_arg1);
            this.m_scrollDisplay.addChild(_arg1);
            this.m_numberOfItems++;
            this.forceDisplayUpdate();
            this.updateArrowButtonStates();
            return (_arg1);
        }
        public function removeItem(_arg1:UIComponent):void{
            ArrayUtil.removeValueFromArray(this.m_items, _arg1);
            this.m_numberOfItems--;
            this.m_scrollDisplay.removeChild(_arg1);
            _arg1 = null;
            if (((((this.m_numberOfItems - this.m_scrollIndex) == (this.m_numberOfVisibleItems - 1))) && (!((this.m_scrollIndex == 0))))){
                this.scrollToIndex((this.m_scrollIndex - 1));
            };
            this.forceDisplayUpdate();
            this.updateArrowButtonStates();
        }
        public function reset():void{
            var _local2:UIComponent;
            var _local1:Array = this.m_items.concat();
            for each (_local2 in _local1) {
                this.removeItem(_local2);
            };
        }
        public function get numberOfVisibleItems():int{
            return (this.m_numberOfVisibleItems);
        }
        public function get numberOfItems():uint{
            return (this.m_numberOfItems);
        }
        public function get isScrolling():Boolean{
            return (this.m_isScrolling);
        }
        public function get items():Array{
            return (this.m_items);
        }
        public function scrollToIndex(_arg1:int, _arg2:Boolean=true):void{
            var _local3:Number;
            var _local4:int;
            var _local5 = -1;
            var _local6:int;
            var _local7:int = (this.m_numberOfItems - this.m_numberOfVisibleItems);
            if ((((_arg1 < (this.m_scrollIndex + this.m_numberOfVisibleItems))) && ((_arg1 > this.m_scrollIndex)))){
                return;
            };
            if (_arg1 > _local7){
                _arg1 = _local7;
            } else {
                if (_arg1 < _local6){
                    _arg1 = _local6;
                };
            };
            _local3 = (_arg1 * this.m_scrollItemWidthPlusMargin);
            _local4 = (_local5 * _local3);
            if (_arg2){
                this.doTweenToTargetPosition(_local4);
            } else {
                this.m_scrollDisplay.left = _local4;
            };
            this.m_scrollIndex = _arg1;
            this.updateArrowButtonStates();
        }
        override protected function createChildren():void{
            this.m_leftArrowButton = SimpleButton(addComponent("scrollArrowButton leftArrowButton", null, SimpleButton));
            this.m_leftArrowButton.addEventListener(MouseEvent.CLICK, this.arrowButton_click);
            this.m_scrollContainer = addComponent("scrollContainer");
            this.m_scrollDisplay = this.m_scrollContainer.addComponent("scrollDisplay");
            this.m_rightArrowButton = SimpleButton(addComponent("scrollArrowButton rightArrowButton", null, SimpleButton));
            this.m_rightArrowButton.addEventListener(MouseEvent.CLICK, this.arrowButton_click);
            this.updateArrowButtonStates();
        }
        protected function doTweenToTargetPosition(_arg1:int):void{
            var _local2:SimpleTween = new SimpleTween(700);
            _local2.addTweenProperty(this.m_scrollDisplay, "left", this.m_scrollDisplay.left, _arg1, Quad.easeInOut);
            _local2.addEventListener(Event.COMPLETE, this.scrollTween_complete);
            _local2.execute();
        }
        protected function updateArrowButtonStates():void{
            if (this.isAtStart()){
                this.m_leftArrowButton.addCSSClass("disable");
                this.m_leftArrowButton.enabled = false;
            } else {
                this.m_leftArrowButton.removeCSSClass("disable");
                this.m_leftArrowButton.enabled = true;
            };
            if (this.isAtEnd()){
                this.m_rightArrowButton.addCSSClass("disable");
                this.m_rightArrowButton.enabled = false;
            } else {
                this.m_rightArrowButton.removeCSSClass("disable");
                this.m_rightArrowButton.enabled = true;
            };
        }
        protected function isAtStart():Boolean{
            return ((this.m_scrollIndex == 0));
        }
        protected function isAtEnd():Boolean{
            return ((this.m_scrollIndex == (this.m_numberOfItems - this.m_numberOfVisibleItems)));
        }
        protected function arrowButton_click(_arg1:MouseEvent):void{
            if (this.m_isScrolling){
                return;
            };
            this.m_isScrolling = true;
            var _local2:SimpleButton = SimpleButton(_arg1.currentTarget);
            if (_local2.hasCSSClass("leftArrowButton")){
                this.scrollRight();
            } else {
                if (_local2.hasCSSClass("rightArrowButton")){
                    this.scrollLeft();
                };
            };
        }
        protected function scrollRight():void{
            this.scrollToIndex((this.m_scrollIndex - this.m_numberOfVisibleItems));
        }
        protected function scrollLeft():void{
            this.scrollToIndex((this.m_scrollIndex + this.m_numberOfVisibleItems));
        }
        protected function measureScrollItemWidthPlusMargin():void{
            var _local1:UIComponent = UIComponent(this.m_scrollDisplay.children()[1]);
            if (_local1){
                this.m_scrollItemWidthPlusMargin = (_local1.width + _local1.style.marginLeft);
            };
        }
        protected function forceDisplayUpdate():void{
            if (this.m_numberOfItems == 2){
                this.m_scrollDisplay.addEventListener(DisplayEvent.VALIDATION_COMPLETE, this.scrollDisplay_validationComplete);
                return;
            };
            this.setScrollContainerWidth();
            this.setScrollDisplayWidth();
        }
        protected function scrollDisplay_validationComplete(_arg1:DisplayEvent):void{
            this.m_scrollDisplay.removeEventListener(DisplayEvent.VALIDATION_COMPLETE, this.scrollDisplay_validationComplete);
            this.measureScrollItemWidthPlusMargin();
            this.setScrollContainerWidth();
            this.setScrollDisplayWidth();
        }
        protected function scrollTween_complete(_arg1:Event):void{
            this.m_isScrolling = false;
        }
        private function setScrollContainerWidth():void{
            this.m_scrollContainer.width = (this.numberOfVisibleItems * this.m_scrollItemWidthPlusMargin);
        }
        private function setScrollDisplayWidth():void{
            this.m_scrollDisplay.width = (this.m_numberOfItems * this.m_scrollItemWidthPlusMargin);
        }
        protected function hideNotNeededItems():void{
            var _local2:UIComponent;
            var _local3:int;
            var _local4:int;
            var _local1:int = this.items.length;
            if (this.m_scrollIndex > this.m_numberOfVisibleItems){
                _local3 = (this.m_scrollIndex - this.m_numberOfVisibleItems);
            } else {
                _local3 = 0;
            };
            if ((this.m_scrollIndex + (2 * this.m_numberOfVisibleItems)) < this.m_numberOfItems){
                _local4 = (this.m_scrollIndex + (2 * this.m_numberOfVisibleItems));
            } else {
                _local4 = _local1;
            };
            var _local5:int;
            while (_local5 < _local1) {
                _local2 = this.items[_local5];
                if ((((_local5 >= _local3)) && ((_local5 <= _local4)))){
                    _local2.show();
                } else {
                    _local2.hide();
                };
                _local5++;
            };
        }

    }
}//package net.wooga.pets.views.components.mainmenu 
